﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace GMS.BIZ
{
    public class Englishs
    {
        private string studentID;

        /// <summary>
        /// 初始化Englishs
        /// </summary>
        /// <param name="studentID">学生学号</param>
        public Englishs(string studentID)
        {
            this.studentID = studentID;
        }

        /// <summary>
        /// 数该学生有多少条英语能力的记录
        /// </summary>
        public int Count()
        {
            GMS.ORM.DAL.GEnglish optGEnglish = new GMS.ORM.DAL.GEnglish();
            DataSet ds = optGEnglish.GetList("std_id='" + this.studentID + "' ORDER BY english_time DESC");
            try
            {
                return ds.Tables[0].Rows.Count;
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 添加一条学生英语能力
        /// </summary>
        /// <param name="inputEnglish">英语的对象</param>
        public string Add(English inputEnglish)
        {
            GMS.ORM.Model.GEnglish myGEnglish = new GMS.ORM.Model.GEnglish();
            myGEnglish.std_id = this.studentID;
            myGEnglish.english_level = inputEnglish.English_level;
            myGEnglish.english_time = inputEnglish.English_time;
            myGEnglish.english_mark = inputEnglish.English_mark;
            
            GMS.ORM.DAL.GEnglish optGEnglish = new GMS.ORM.DAL.GEnglish();
            bool ok = optGEnglish.Add(myGEnglish);
            if (!ok) return ErrorMessage.DBOptError;
            else return ErrorMessage.OK;
        }

        /// <summary>
        /// 删除一条学生英语能力
        /// </summary>
        /// <param name="id">删除的id号</param>
        public string Delete(decimal id)
        {
            GMS.ORM.DAL.GEnglish optGEnglish = new GMS.ORM.DAL.GEnglish();
            bool ok = optGEnglish.Delete(id);
            if (!ok) return ErrorMessage.DBOptError;
            else return ErrorMessage.OK;
        }

        /// <summary>
        /// 修改一条学生英语能力
        /// </summary>
        /// <param name="inputEnglish">英语的对象</param>
        public string Update(English inputEnglish)
        {
            GMS.ORM.Model.GEnglish myGEnglish = new GMS.ORM.Model.GEnglish();
            myGEnglish.id = inputEnglish.Id;
            myGEnglish.std_id = this.studentID;
            myGEnglish.english_level = inputEnglish.English_level;
            myGEnglish.english_time = inputEnglish.English_time;
            myGEnglish.english_mark = inputEnglish.English_mark;

            GMS.ORM.DAL.GEnglish optGEnglish = new GMS.ORM.DAL.GEnglish();
            bool ok = optGEnglish.Update(myGEnglish);
            if (!ok) return ErrorMessage.DBOptError;
            else return ErrorMessage.OK;
        }

        /// <summary>
        /// 获得一条学生的英语能力
        /// </summary>
        /// <param name="id">id</param>
        public English GetOne(decimal id)
        {
            English outputEnglish = new English();
            GMS.ORM.Model.GEnglish myGEnglish = new GMS.ORM.Model.GEnglish();
            GMS.ORM.DAL.GEnglish optGEnglish = new GMS.ORM.DAL.GEnglish();
            try
            {
                myGEnglish = optGEnglish.GetModel(id);
                outputEnglish.Id = myGEnglish.id;
                outputEnglish.StudentID = myGEnglish.std_id;
                outputEnglish.English_level = myGEnglish.english_level;
                outputEnglish.English_time = myGEnglish.english_time;
                outputEnglish.English_mark = myGEnglish.english_mark;
                return outputEnglish;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 获得一个学生的英语能力的数组
        /// </summary>
        public ArrayList GetList()
        {
            ArrayList returnList = new ArrayList();

            GMS.ORM.DAL.GEnglish optGEnglish = new GMS.ORM.DAL.GEnglish();
            DataSet ds = optGEnglish.GetList("std_id='" + this.studentID + "' ORDER BY english_time DESC");
            if(ds.Tables[0].Rows.Count==0)
                return returnList;
            else
            {
                int howMany=ds.Tables[0].Rows.Count;
                for(int i=0;i<howMany;i++)
                    returnList.Add(decimal.Parse(ds.Tables[0].Rows[i]["id"].ToString()));
                return returnList;
            }
        }

        /// <summary>
        /// 获得一个学生的英语能力的DataSet
        /// </summary>
        public DataSet GetDataSet()
        {
            GMS.ORM.DAL.GEnglish optGEnglish=new GMS.ORM.DAL.GEnglish();
            DataSet ds= optGEnglish.GetList("std_id='"+this.studentID+"' ORDER BY english_time DESC");
            return ds;
        }

        /// <summary>
        /// 返回学生的英语能力
        /// </summary>
        /// <param name="studentID">学生学号，可以为空</param>
        /// <param name="english_level">英语能力，可以为空</param>
        /// <param name="after">在此时间之后，可以为空</param>
        /// <param name="before">在此时间之前，可以为空</param>
        /// <param name="moreThan">分数高于，可以为空</param>
        /// <param name="lessThan">分数低于，可以为空</param>
        static public DataSet GetDataSet(string studentID, string english_level, DateTime? after, DateTime? before, int? moreThan, int? lessThan)
        {
            string queryString = "1=1";
            if (studentID != string.Empty) queryString = queryString + " AND std_id='" + studentID + "'";
            if (english_level != string.Empty) queryString = queryString + " AND english_level='" + english_level + "'";
            if (after != null) queryString = queryString + " AND english_time>" + after.ToString();
            if (before != null) queryString = queryString + " AND english_time<" + before.ToString();
            if (moreThan != null) queryString = queryString + " AND english_mark>" + moreThan.ToString();
            if (lessThan != null) queryString = queryString + " AND english_mark<" + lessThan.ToString();

            GMS.ORM.DAL.GEnglish optGEnglish = new GMS.ORM.DAL.GEnglish();
            return optGEnglish.GetList(queryString + " ORDER BY english_time");
        }
    }
}